package com.webserver.http;
import static com.webserver.http.HttpContext.CR;//静态导入
import static com.webserver.http.HttpContext.LF;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
         * 该类的每一个实例用于表示一个HTTP协议规定的响应内容。
         * 每个响应由三部分构成:
         * 状态行，响应头，响应正文
         */
public class HttpServletResponse {
    //响应头的相关信息
    //key:响应头的名字 value：响应头对应的值
    private Map<String,String> headers = new HashMap<>();
    //状态行相关信息
    private int statusCode=200;//状态代码
    private String statusReason = "OK";//状态描述
    //响应正文的相关信息
    private File contentFile;
    private Socket socket;
    public HttpServletResponse(Socket socket){
        this.socket=socket;
    }
    /**
     * 发送响应
     * 降当前响应对象内容按照标准的响应格式发给客户端
     */
    public void response()throws IOException {
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //3.3发送响应正文
        sendContent();
        }
    private void sendStatusLine()throws IOException{
        String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
        println(line);
        System.out.println("发送状态行:"+line);
    }
    private void sendHeaders()throws IOException {
//        String line = "Content-Type: text/html";
//        println(line);
//        System.out.println("发送响应头:"+line);
//
//        line = "Content-Length: " + contentFile.length();
//        println(line);
//        System.out.println("发送响应头:"+line);
        Set<Map.Entry<String,String>> entrySet = headers.entrySet();
        for(Map.Entry<String,String> e : entrySet){
            String name = e.getKey();//获取响应头的名字
            String value = e.getValue();//获取响应头的值
            String line = name + ": " + value;
            println(line);
            System.out.println("响应头："+line);
        }
        println("");//重用println方法输出回车+换行
    }
    private void sendContent()throws IOException {
        OutputStream osp = socket.getOutputStream();
        byte[] buf = new byte[1024 * 10];
        int len;
        try(//try之后编译器默认会有一个finally关闭流
            FileInputStream fis = new FileInputStream(contentFile);
            )
        {
            while ((len = fis.read(buf)) != -1) {
                osp.write(buf, 0, len);
            }
        }
//        osp.write(13);
//        osp.write(10);
//        osp.write(13);
//        osp.write(10);
        System.out.println("响应正文发送完毕!");
    }
    private void println(String line)throws IOException {
        OutputStream os  = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        os.write(data);
        os.write(CR);
        os.write(LF);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
        String ext = (String.valueOf(contentFile.getName().split("\\.")[
                Integer.parseInt(String.valueOf(contentFile.getName().split("\\.").length))-1
                ]));//获取后缀名
        String type = HttpContext.getContentType(ext);
        addHeader("Content-Type",type);
        addHeader("Content-Length",String.valueOf(contentFile.length()));
    }

    /**
     * 添加一个要发送的响应头
     * @param name
     * @param value
     */
    public void addHeader(String name,String value){
        this.headers.put(name,value);
    }
}
